
package net.swa.dewei.web.action;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import net.swa.dewei.beans.entity.CheXing;
import net.swa.dewei.beans.entity.ChenxingCityRelation;
import net.swa.dewei.beans.entity.District;
import net.swa.dewei.service.CheXingCityService;
import net.swa.system.service.ICommonService;
import net.swa.system.util.annotation.Log;
import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.BuzConst;
import net.swa.util.DateUtils;
import net.swa.util.JsonResult;

/** 车型城市信息维护 Action **/
public class CheXingCityAction extends AbstractBaseAction
{
    private static final long serialVersionUID = 3791594428014981760L;

    /**车型城市关系操作维护类**/
    private CheXingCityService cheXingCityService;

    /** 公用方法接口 */
    private ICommonService commonService;

    private CheXing bean;

    private ChenxingCityRelation model;

    /**省列表**/
    private List<District> provinceList;

    /**市列表*/
    private List<District> cityList;

    /**县区列表*/
    private List<District> areaList;

    /**车型信息列表**/
    private List<CheXing> chexingList;

    /**省**/
    private String province;

    /**市列表*/
    private String city;

    /**县区列表*/
    private String town;

    /**车型名称**/
    private String chexingName;

    // 实体类id
    private Long id;

    // id集合
    private Long[] ids;

    //电池图片
    private String[] imgUrls;

    /** 初始化分页查询 **/
    @Log(description = "初始化车型城市分页查询")
    public String listPage()
    {
        provinceList = commonService.search("pid", "0", District.class);
        return "listPage";
    }

    /**根据城市关系查询车型，分页展示
     * @throws Exception ***/
    public String queryPage() throws Exception
    {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 20 : pageSize;
        JsonResult<ChenxingCityRelation> json = null;
        json = cheXingCityService.queryCheXingPage(province, city, town, chexingName, currentPage, pageSize, orderBy,
                orderType);
        json.setCurrentPage(currentPage);
        outJson(json);
        return null;
    }

    /** 去新增或者修改页面 **/
    @Log(description = "去新增城市车型页面")
    public String toAddCheXingCity() throws Exception
    {
        provinceList = commonService.search("pid", "0", District.class);
        chexingList = commonService.search(null, null, CheXing.class);
        return "toAddCheXingCity";
    }

    @Log(description = "去按城市编辑车型页面")
    public String toEditByCity()
    {

        chexingList = cheXingCityService.queryListByCity(province, city, town);
        return "toEditByCity";
    }

    @Log(description = "去修改城市车型页面")
    public String toUpdateCheXingCity() throws Exception
    {
        if (null != id)
        {
            model = cheXingCityService.queryById(id);
        }
        else
        {
            model = null;
        }
        return "toUpdateCheXingCity";
    }

    /** 新增或者修改 **/
    @Log(description = "新增或者修改车型")
    public String saveCheXingCity() throws Exception
    {

        List<ChenxingCityRelation> list = new ArrayList<ChenxingCityRelation>();
        if (null != ids && ids.length > 0)
        {
            for (int i = 0; i < ids.length; i++)
            {
                if (null != ids[i])
                {
                    ChenxingCityRelation r = new ChenxingCityRelation();
                    r.setCity(city);
                    r.setProvince(province);
                    //                    r.setTown(town);
                    r.setChexingId(ids[i]);
                    r.setSort(i + 1);
                    if (null != imgUrls && (!StringUtils.isBlank(imgUrls[i])))
                    {
                        r.setImageUrl(imgUrls[i]);
                    }
                    else
                    {
                        r.setImageUrl(null);
                    }
                    r.setCrateDate(DateUtils.getCurrDate(null));
                    r.setUpdateDate(DateUtils.getCurrDate(null));
                    list.add(r);
                }
            }
        }
        cheXingCityService.batchAdd(list, province, city, town);
        outSuccess();
        return null;
    }

    public String saveUpdateCheXingCity() throws Exception
    {
        if (null != model)
        {
            if (cheXingCityService.update(model))
            {
                outSuccess();
            }
            else
            {
                outError("该信息已经被删除");
            }

        }
        else
        {
            outError("信息已经被别的用户删除");
        }
        return null;
    }

    /** * 删除方法 **/
    @Log(description = "删除车型")
    public String delete() throws Exception
    {
        if (cheXingCityService.delete(ids))
        {
            outSuccess();
        }
        else
        {
            outError("删除失败");
        }

        return null;
    }

    /**查看城市所选的车型
     * @throws Exception **/
    public String queryCityChexingIds() throws Exception
    {
        JsonResult<Long> json = new JsonResult<Long>();
        if (!StringUtils.isBlank(city))
        {
            List<Long> ids = cheXingCityService.queryChexingIdsByCity(city);
            json.setResult(ids);
            json.setSuccess(true);
        }
        else
        {
            json.setSuccess(false);
            json.setMessage("传入city:" + city);
        }
        outJson(json);
        return null;
    }

    @Log(description = "进入车型信息综合查询页面")
    public String queryChexingTotal() throws Exception
    {
        provinceList = commonService.search("pid", "0", District.class);
        return "queryChexingTotal";
    }

    @Log(description = "综合查询车型信息数据")
    public String searchChexing() throws Exception
    {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 20 : pageSize;
        if (StringUtils.isBlank(province))
        {
            JsonResult<CheXing> json = null;
            String[] attrNames = { "state" };
            Object[] attrValues = { BuzConst.CHEXING_STATE_SHOW };
            json = commonService
                    .search(attrNames, attrValues, CheXing.class, currentPage, pageSize, orderBy, orderType);
            json.setCurrentPage(currentPage);
            outJson(json);
        }
        else
        {
            JsonResult<ChenxingCityRelation> json = null;
            json = cheXingCityService.queryCheXingPage(province, city, town, chexingName, currentPage, pageSize,
                    orderBy, orderType);
            json.setCurrentPage(currentPage);
            outJson(json);
        }
        return null;
    }

    //根据城市查询车型
    public String queryByCity() throws Exception
    {
        List<CheXing> list = null;
        if (!StringUtils.isBlank(city))
        {
            list = cheXingCityService.queryChexByCity(province, city, town);
            if (null != list && list.size() < 1)
            {
                list = commonService.search("state", BuzConst.CHEXING_STATE_SHOW, CheXing.class, null, "sort", "asc");
            }
        }
        else
        {
            list = commonService.search("state", BuzConst.CHEXING_STATE_SHOW, CheXing.class, null, "sort", "asc");
        }
        outJson(list);
        return null;
    }

    public Long getId()
    {

        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Long[] getIds()
    {
        return ids;
    }

    public void setIds(Long[] ids)
    {
        this.ids = ids;
    }

    @Required
    @Resource
    public void setCheXingCityService(CheXingCityService cheXingCityService)
    {
        this.cheXingCityService = cheXingCityService;
    }

    public ICommonService getCommonService()
    {
        return commonService;
    }

    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    public CheXing getBean()
    {
        return bean;
    }

    public void setBean(CheXing bean)
    {
        this.bean = bean;
    }

    public ChenxingCityRelation getModel()
    {
        return model;
    }

    public void setModel(ChenxingCityRelation model)
    {
        this.model = model;
    }

    public List<District> getProvinceList()
    {
        return provinceList;
    }

    public void setProvinceList(List<District> provinceList)
    {
        this.provinceList = provinceList;
    }

    public List<District> getCityList()
    {
        return cityList;
    }

    public void setCityList(List<District> cityList)
    {
        this.cityList = cityList;
    }

    public List<District> getAreaList()
    {
        return areaList;
    }

    public void setAreaList(List<District> areaList)
    {
        this.areaList = areaList;
    }

    public String getProvince()
    {
        return province;
    }

    public void setProvince(String province)
    {
        this.province = province;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getTown()
    {
        return town;
    }

    public void setTown(String town)
    {
        this.town = town;
    }

    public String getChexingName()
    {
        return chexingName;
    }

    public void setChexingName(String chexingName)
    {
        this.chexingName = chexingName;
    }

    public List<CheXing> getChexingList()
    {
        return chexingList;
    }

    public void setChexingList(List<CheXing> chexingList)
    {
        this.chexingList = chexingList;
    }

    public String[] getImgUrls()
    {
        return imgUrls;
    }

    public void setImgUrls(String[] imgUrls)
    {
        this.imgUrls = imgUrls;
    }

}
